package com.demo;

import java.util.*;

public class MapDemo2 {
    public static void main(String[] args) {

//        listWrapList();
//        listWrapMap();
//        mapWrapMap();

        // 斗地主

        // 1. 准备牌：组装 54 张扑克牌
        HashMap<Integer,String> pokers = new HashMap<>();
        prepare(pokers);

        // 2. 洗牌：54 张牌顺序打乱
        List<Integer> shuffleIndexs = shuffle(pokers);

        // 3. 发牌：三个玩家参与游戏，三人交替摸牌，每人 17 张牌，最后三张留作底牌
        LinkedHashMap<String, TreeSet<Integer>> linkedHashMap = deal(pokers, shuffleIndexs);

        // 看牌：查看三人各自手中的牌（按照牌的大小排序）、底牌
        String result = view(linkedHashMap, pokers);
        System.out.println(result);

    }

    private static String view(LinkedHashMap<String, TreeSet<Integer>> linkedHashMap, HashMap<Integer, String> pokers) {
        Set<String> roleSet = linkedHashMap.keySet();
        StringBuilder sb = new StringBuilder();
        for (String role : roleSet) {
            sb.append(role).append(": ");
            TreeSet<Integer> cardIndexSet = linkedHashMap.get(role);
            for (Integer index : cardIndexSet) {
                sb.append(pokers.get(index)).append("  ");
            }
            sb.append("\n");
        }
        return sb.toString();
    }

    private static LinkedHashMap<String, TreeSet<Integer>> deal(HashMap<Integer, String> pokers, List<Integer> indexs) {
        LinkedHashMap<String, TreeSet<Integer>> linkedHashMap = new LinkedHashMap();
        TreeSet<Integer> bottom = new TreeSet<>();
        TreeSet<Integer> play1 = new TreeSet<>();
        TreeSet<Integer> play2 = new TreeSet<>();
        TreeSet<Integer> play3 = new TreeSet<>();
        // 预留三张底牌
        // 发牌
        for (int i = 0; i < indexs.size() - 3; i++) {
            if (i % 3 == 0) {
                play1.add(indexs.get(i));
            } else if (i % 3 == 1) {
                play2.add(indexs.get(i));
            } else if (i % 3 == 2) {
                play3.add(indexs.get(i));
            }
        }
        // 底牌
        bottom.add(indexs.get(50));
        bottom.add(indexs.get(51));
        bottom.add(indexs.get(52));

        linkedHashMap.put("玩家一", play1);
        linkedHashMap.put("玩家二", play2);
        linkedHashMap.put("玩家三", play3);
        linkedHashMap.put("底牌", bottom);
        return linkedHashMap;
    }

    private static List<Integer> shuffle(HashMap<Integer, String> pokers) {
        Set<Integer> indexSet = pokers.keySet();
        List<Integer> lists = new ArrayList<>();
        // 打乱的是索引
        lists.addAll(indexSet);
        Collections.shuffle(lists);
        return lists;
    }

    private static void prepare(HashMap<Integer,String> pokers) {
        String[] colors = {"♦️", "♣️", "♥️", "♠️"};
        String[] nums = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};

        pokers.put(0, "RED JOKER");
        pokers.put(1, "BLACK JOKER");

        int count = 2;
        for (String num : nums) {
            for (String color : colors) {
                pokers.put(count++, color + num);
            }
        }
    }

    private static void mapWrapMap() {
        // 1. 创建年级集合
        HashMap<String, HashMap<String, String>> grade = new HashMap();


        // 2. 创建班级集合
        HashMap<String, String> class1Grade3 = new HashMap<>();
        class1Grade3.put("it001", "迪丽热巴");
        class1Grade3.put("it002", "古力娜扎");
        class1Grade3.put("it003", "玛尔扎哈");
        class1Grade3.put("it004", "欧阳娜娜");

        HashMap<String, String> class2Grade3 = new HashMap<>();
        class2Grade3.put("it001", "李小璐");
        class2Grade3.put("it002", "白百合");
        class2Grade3.put("it003", "马蓉");


        HashMap<String, String> class3Grade3 = new HashMap<>();
        class3Grade3.put("it001", "林丹");
        class3Grade3.put("it002", "文章");
        class3Grade3.put("it003", "陈赫");

        grade.put("三年级一班", class1Grade3);
        grade.put("三年级二班", class2Grade3);
        grade.put("三年级三班", class3Grade3);

        Set<String> clsNames = grade.keySet();
        for (String clsName : clsNames) {
            System.out.println("-------【" + clsName + " 】--------");
            Map<String, String> cls = grade.get(clsName);
            Set<String> ids = cls.keySet();
            for (String id : ids) {
                String name = cls.get(id);
                System.out.println(id + "--->" + name);
            }

        }
    }

    private static void listWrapMap() {
        // 1. 创建年级集合
        ArrayList<HashMap<String, String>> grade = new ArrayList<HashMap<String, String>>();

        // 2. 创建班级集合
        HashMap<String, String> class1Grade3 = new HashMap<>();
        class1Grade3.put("it001", "迪丽热巴");
        class1Grade3.put("it002", "古力娜扎");
        class1Grade3.put("it003", "玛尔扎哈");
        class1Grade3.put("it004", "欧阳娜娜");

        HashMap<String, String> class2Grade3 = new HashMap<>();
        class2Grade3.put("it001", "李小璐");
        class2Grade3.put("it002", "白百合");
        class2Grade3.put("it003", "马蓉");


        HashMap<String, String> class3Grade3 = new HashMap<>();
        class3Grade3.put("it001", "林丹");
        class3Grade3.put("it002", "文章");
        class3Grade3.put("it003", "陈赫");

        Collections.addAll(grade, class1Grade3, class2Grade3, class3Grade3);

        for (HashMap<String, String> cls : grade) {
            Set<String> ids = cls.keySet();
            for (String id : ids) {
                String name = cls.get(id);
                System.out.println(id + "->" + name);
            }

            System.out.println("================");
        }
    }

    private static void listWrapList() {
        // 1. 创建班级
        ArrayList<String> class1Grade3 = new ArrayList<>();
        ArrayList<String> class2Grade3 = new ArrayList<>();
        ArrayList<String> class3Grade3 = new ArrayList<>();

        // 2. 添加学生
        Collections.addAll(class1Grade3, "迪丽热巴", "古力娜扎", "玛尔扎哈", "欧阳娜娜");
        Collections.addAll(class2Grade3, "李小璐", "白百合", "马蓉");
        Collections.addAll(class3Grade3, "林丹", "文章", "陈赫");


        // 3. 添加班级
        ArrayList<ArrayList<String>> grade = new ArrayList<>();
        Collections.addAll(grade, class1Grade3, class2Grade3, class3Grade3);

        // 4. 打印每个班级的学生
        for (ArrayList<String> cls : grade) {
            for (String name : cls) {
                System.out.println(name);
            }
            System.out.println("-----------------");
        }
    }
}
